﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevFx.Web.Code;
using DevFx.Model;
using DevFx.FrameWork;

namespace DevFx.Web.Pages.Rights
{
    public partial class UserEdit : BasePage
    {
        #region 变量

        #endregion

        #region 属性

        /// <summary>
        /// ID
        /// </summary>
        public Guid MID
        {
            get
            {

                if (ViewState["MID"] == null)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                    {
                        ViewState["MID"] = Request.QueryString["id"].ToString();
                    }
                    else
                    {
                        ViewState["MID"] = Guid.Empty;
                    }
                }
                Guid g = new Guid(ViewState["MID"].ToString());
                return g;
            }
            set { ViewState["MID"] = value; }
        }
        /// <summary>
        /// 当前操作的对象
        /// </summary>    
        private SysUser EntityInfo
        {
            get
            {
                if (ViewState["EntityInfo"] == null)
                {
                    if (!(MID == Guid.Empty))
                    {
                        using (Entities db = new Entities())
                        {
                            ViewState["EntityInfo"] = db.SysUsers.Where(m => m.UserID == MID).FirstOrDefault();
                        }
                    }
                    else
                    {
                        SysUser entity = new SysUser();
                        if (MID == Guid.Empty)
                        {
                            entity.UserID = Guid.NewGuid();
                        }
                        entity.CreateBy = UserID;
                        entity.CreateDate = DateTime.Now;
                        entity.ModifyBy = UserID;
                        entity.ModifyDate = DateTime.Now;
                        ViewState["EntityInfo"] = entity;
                    }
                }
                return ViewState["EntityInfo"] as SysUser;
            }
            set
            {
                ViewState["EntityInfo"] = value;
            }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            using (Entities db = new Entities())
            {
                // GetData();
                if (IsAdd)
                {
                    GetData(EntityInfo);
                    db.AddToSysUsers(EntityInfo);
                }
                else if (IsEdit)
                {
                    var entity = db.SysUsers.FirstOrDefault(m => m.UserID == MID);
                    GetData(entity);
                }
                db.SaveChanges();
            }
            MessageBox.ShowAndCloseRefrashJQueryDialog(this.Page, "保存成功！", "");

        }
        #endregion

        #region 方法
        private void BindData()
        {
            this.lblID.Text = EntityInfo.UserID.ToString();
            if (IsEdit)
            {
                this.tbxName.Text = EntityInfo.UserName == null ? "" : EntityInfo.UserName.ToString();
                this.cbxDisabled.Checked = EntityInfo.Disabled.Value;
                this.tbxMail.Text = EntityInfo.Email;
            }
        }
        private void GetData(SysUser entity)
        {
            entity.UserName = this.tbxName.Text.Trim();
            entity.Disabled = this.cbxDisabled.Checked;
            entity.Email = this.tbxMail.Text.Trim();
            entity.ModifyBy = UserID;
            entity.ModifyDate = DateTime.Now;
        }
        #endregion

      
    }
}